package edu.ymu.class1.action;


import edu.ymu.class1.dao.AreaDao;
import edu.ymu.class1.dao.CityDao;
import edu.ymu.class1.dao.ProvinceDao;
import edu.ymu.class1.dao.StudentDao;
import edu.ymu.class1.model.Student;
import edu.ymu.class1.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

@Controller
@CrossOrigin
public class PublicAction {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private CityDao cityDao;

    @Autowired
    private AreaDao areaDao;


    @GetMapping("testDao")
    @ResponseBody
    public boolean testDao() throws Exception{
        //添加
//        Student student=new Student();
//        student.setStunum("201621447112");
//        student.setName("马荣志");
//        student.setSex("男");
//        student.setBirthday(new Timestamp(System.currentTimeMillis()));
//        student.setNativeplace("云南玉溪");
//        studentDao.save(student);

        //修改
//        Student student=new Student();
//        student.setStudentid("88ba7b33-3e7c-43c7-beda-6a43de1689b5");
//        student.setStunum("201621447102");
//        student.setName("姓名修改");
//
//        studentDao.save(student);

        //查询单个
//        Student student=studentDao.findOne("88ba7b33-3e7c-43c7-beda-6a43de1689b5");
//        student.setSex("M");
//        student.setBirthday(new Timestamp(System.currentTimeMillis()));
//        student.setNativeplace("红塔区");
//        studentDao.save(student);

        //删除
//        Student student=new Student();
//        student.setStudentid("88ba7b33-3e7c-43c7-beda-6a43de1689b5");
//        studentDao.delete(student);

        return true;

    }


    @GetMapping("getAnObject")
    @ResponseBody
    public Student getAnObject() throws Exception{

        List<Student> sl=studentDao.findAll();
        if(sl.size()>0){
            return sl.get(0);
        }
        else {
            return null;
        }

    }



    @PostMapping("saveStudentInfo")
    @ResponseBody
    public Map saveStudentInfo(@RequestBody StudentVo studentVo)throws Exception{
        Map m=new HashMap();

        String sf=studentVo.getSfzh().substring(0,2);
        String sfname=provinceDao.getProvinceNameByAdcode(sf+"0000");

        String zs=studentVo.getSfzh().substring(2,4);
        String zsname=cityDao.getCityNameByZSCoda(sf+zs+"00");

        String qx=studentVo.getSfzh().substring(4,6);
        String parm=sf+zs+qx;
        String qxname=areaDao.getAreaNameByQxCode(parm);

        String csrq=studentVo.getSfzh().substring(6,14);
        Timestamp csrqtime=this.getBirthTimeByStr(csrq);

        String xb=studentVo.getSfzh().substring(16,17);
        String xbname=this.getXbByCode(xb);

        //待保存到数据库中的对象
        Student student=new Student();
        if(studentVo.getStudentid()!=null&&!studentVo.getStudentid().equals("")){
            student.setStudentid(studentVo.getStudentid());
        }


        student.setNativeplace(sfname+zsname+qxname);
        student.setBirthday(csrqtime);
        student.setSex(xbname);
        student.setName(studentVo.getXm());
        student.setStunum(studentVo.getXh());


        studentDao.save(student);


        m.put("province",sfname);
        m.put("city",zsname);
        m.put("area",qxname);
        m.put("birthday",csrq);
        m.put("sex",xbname);


        return m;


    }


    private String getXbByCode(String xb){
        int sexi=Integer.parseInt(xb);
        if(sexi%2==0){
            return "女";
        }
        else{
            return "男";
        }

    }


    private Timestamp getBirthTimeByStr(String birthStr){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(birthStr, pos);
        return new Timestamp( strtodate.getTime());


    }

    @GetMapping("getStudentList")
    @ResponseBody
    public List<Student> getStudentList() throws Exception{

        List<Student> sl=studentDao.findAll();

        return sl;

    }


    @PostMapping("getSingleStudentById")
    @ResponseBody
    public Student getSingleStudentById(@RequestBody StudentVo studentVo) throws Exception{

        Student student=studentDao.findOne(studentVo.getStudentid());

        return student;

    }


    @PostMapping("deleteStudent")
    @ResponseBody
    public boolean deleteStudent(@RequestBody StudentVo studentVo) throws Exception{

        try {
            studentDao.delete(studentVo.getStudentid());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }




}
